<!--
  author: tangcheng_cd
  create by: 2018/6/6
  descr: 物理表表格
-->
<template>
  <div class="meta-table-grid">
    <!--表格-->
    <el-table
      ref="userGrid"
      @row-click="rowClicked"
      @selection-change="handleSelectionChange"
      v-loading="tableLoading"
      highlight-current-row
      :data="pageData.list">
      <!--列信息-->
      <el-table-column v-if="isShowExpand" type="expand">
        <template slot-scope="props">
          <el-form label-position="left" inline class="form-table-expand">
            <el-form-item
              v-for="eCol in expandColumns"
              v-if="displayExpandColumns.indexOf(eCol.prop) !== -1"
              :key="eCol.prop"
              :label="eCol.label">
              <span>{{ eCol.render ? eCol.render(props.row[eCol.prop]) : props.row[eCol.prop] }}</span>
            </el-form-item>
          </el-form>
        </template>
      </el-table-column>
      <!--选择框-->
      <el-table-column
        v-if="checkbox"
        type="selection"
      >
      </el-table-column>

      <el-table-column
        v-for="col in columns"
        v-if="displayColumns.indexOf(col.prop) !== -1"
        :key="col.prop"
        :prop="col.prop"
        :label="col.label"
        :width="col.width || {}"
        :formatter="col.render">
      </el-table-column>
    </el-table>
    <!--分页-->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="search.pageNo"
      :page-sizes="[5, 10, 15, 20]"
      :page-size="search.pageSize"
      :layout="paginationLayout"
      :total="pageData.total">
    </el-pagination>
  </div>
</template>

<script type="text/ecmascript-6">
  import api from '../../../../api/url'
  import ajax from '../../../../api/ajax/ajax'
  import {formatDate} from 'element-ui/packages/date-picker/src/util'

  export default {
    name: 'meta-table-grid',
    data () {
      return {
        tableLoading: false, // 数据是否加载中
        search: {
          type: Object,
          pageNo: 1, // 当前页号
          pageSize: 10, // 每页显示记录数
          keyword: null // 关键字
        },
        pageData: Object, // 当前表格页数据
        currentRow: null, // 当前行
        selections: [] // 所有的选择
      }
    },
    props: {
      // 选择框
      checkbox: {
        type: Boolean,
        default: false
      },
      // 显示的详情列
      displayColumns: {
        type: Array,
        default () {
          return ['meta_id', 'table_name', 'table_name_cns', 'theme_id', 'level_id']
        }
      },
      // 表格列
      columns: {
        type: Array,
        default () {
          return [
            {
              prop: 'meta_id',
              label: '元数据代码'
            },
            {
              prop: 'table_name',
              label: '英文表名'
            },
            {
              prop: 'table_name_cns',
              label: '中文表名'
            },
            {
              prop: 'theme_id',
              label: '所属主题'
            },
            {
              prop: 'level_id',
              label: '所属层级'
            }
          ]
        }
      },
      // 是否显示详情扩展列
      isShowExpand: {
        type: Boolean,
        default: false
      },
      // 显示的扩展列
      displayExpandColumns: {
        type: Array,
        default () {
          return ['meta_id', 'table_name', 'table_name_cns', 'theme_id', 'level_id']
        }
      },
      // 详情扩展列
      expandColumns: {
        type: Array,
        default: () => [
          {
            prop: 'meta_id',
            label: '元数据代码'
          },
          {
            prop: 'table_name',
            label: '英文表名'
          },
          {
            prop: 'table_name_cns',
            label: '中文表名'
          },
          {
            prop: 'theme_id',
            label: '所属主题'
          },
          {
            prop: 'level_id',
            label: '所属层级'
          }
        ]
      },
      // 分页配置
      paginationLayout: {
        type: String,
        default: () => 'total, sizes, prev, pager, next, jumper'
      }
    },
    methods: {
      formatDate (...arg) {
        return formatDate(...arg)
      },
      // 行单击
      rowClicked (row, el) {
        this.currentRow = row
        this.$emit('rowClicked', row)
      },
      // 选择改变
      handleSelectionChange (rows) {
        this.selections = rows
      },
      // 获取所有的选择数据
      getSelections () {
        return this.selections
      },
      // 获取当前选中数据
      getCurrentNode () {
        return this.currentRow
      },
      // 清除选择
      clearSelections () {
        this.currentRow = null
        this.selections = []
      },
      // 页记录数更改
      handleSizeChange (pageSize) {
        this.search.pageNo = 1 // 重置到第一页
        this.search.pageSize = pageSize
        this.getData()
      },
      // 当前页切换
      handleCurrentChange (pageNo) {
        this.search.pageNo = pageNo
        this.getData()
      },
      // 通过关键字搜索
      getDataByKeyword (keyword) {
        this.search.keyword = keyword
        this.getData()
      },
      // 获取页面数据
      getData () {
        // 数据加载中
        this.tableLoading = true
        ajax.post(api.SELECT_METATABLE_LIST_FOR_PAGE, this.search).then(res => {
          this.pageData = res.data
          // 数据加载完毕
          this.tableLoading = false
        }).catch(e => {
          console.error('获取数据发生错误！')
        })
      }
    },
    created () {
      this.getData()
    }
  }
</script>

<style lang="stylus" rel="stylesheet/stylus">
  .meta-table-grid
    height 100%
    background-color #fff
    .el-table
      .el-table__body
        padding-bottom 70px
      .form-table-expand label
        width 90px
        color #99a9bf
      .form-table-expand .el-form-item
        margin-right 0
        margin-bottom 0
        width 50%
    .el-pagination
      z-index 5
      height 36px
      background-color #fff
      position absolute
      width 100%
      bottom 0px
      padding-bottom 4px
      border-top solid 1px #f4f4f4
</style>

<!--<style lang="stylus" rel="stylesheet/stylus">-->
  <!--.meta-table-grid-->
    <!--padding 0px 0px-->
    <!--height 100%-->
    <!--background-color #fff-->
    <!--overflow auto-->
    <!--.el-table-->
      <!--padding-bottom 66px-->
      <!--.form-table-expand-->
        <!--font-size 0-->
      <!--.form-table-expand label-->
        <!--width 90px-->
        <!--color #99a9bf-->
      <!--.form-table-expand .el-form-item-->
        <!--margin-right 0-->
        <!--margin-bottom 0-->
        <!--width 50%-->
    <!--.el-pagination-->
      <!--z-index 5-->
      <!--height 36px-->
      <!--background-color #fff-->
      <!--position absolute-->
      <!--width 100%-->
      <!--bottom 0px-->
      <!--padding-bottom 4px-->
      <!--border-top solid 1px #f4f4f4-->
<!--</style>-->
